package com.aida.evil.eda.MyView;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

/****************************************************
 * com.aida.evil.aida.MyView.a
 * 作者：白雨濃  evil
 * 时间：16-2-9 22:26
 * 邮箱：evilbai@foxmail.com
 * 页面用途：用于主界面左侧划出栏的自定义HorizontalScrollView控件
 ****************************************************/
public class SlidingMenu extends HorizontalScrollView
{

    private static int 自定义主菜单剩余宽度=80;

    private LinearLayout mWapper;
    private ViewGroup mMenu;
    private ViewGroup mContent;

    private int mScreenWidth_屏幕宽度;

    private int mMenuRightPadding_屏幕右侧宽度;//单位dp

    private int mMenuWidth;

    private boolean once = false;

    /**
     * 未使用自定义属性时 调用
     *
     * @param context
     * @param attrs
     */
    public SlidingMenu(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        //获取屏幕宽高
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);

        mScreenWidth_屏幕宽度 = displayMetrics.widthPixels;


        //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        //把dp转化为px
        mMenuRightPadding_屏幕右侧宽度 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 自定义主菜单剩余宽度, context.getResources().getDisplayMetrics());


    }

    /**
     * 自定义ViewGroup
     * 1.onMeasure 决定内部View（子View）的宽和高，以及自己的宽和高
     * 2.onLayout 决定子View放置的位置
     * 3.onTouchEvent 移动效果
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        if (!once)
        {
            mWapper = (LinearLayout) getChildAt(0);
            mMenu = (ViewGroup) mWapper.getChildAt(0);
            mContent = (ViewGroup) mWapper.getChildAt(1);

            mMenuWidth = mMenu.getLayoutParams().width = mScreenWidth_屏幕宽度 - mMenuRightPadding_屏幕右侧宽度;
            mContent.getLayoutParams().width = mScreenWidth_屏幕宽度;
            once = true;
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    /**
     * 通过设置偏移量，将Menu隐藏
     */

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        super.onLayout(changed, l, t, r, b);

        if (changed)
        {
            this.scrollTo(mMenuWidth, 0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev)
    {
        int action = ev.getAction();
        switch (action)
        {
            case MotionEvent.ACTION_UP:
                //隐藏在左边的宽度
                int scrollX = getScrollX();
                if (scrollX >= mMenuWidth / 2)
                {
                    this.smoothScrollTo(mMenuWidth, 0);//带动画效果的ScrollTo，ScrollTo是瞬间完成的
                }
                else
                {
                    this.smoothScrollTo(0, 0);
                }
                return true;
        }


        return super.onTouchEvent(ev);
    }
}



